package com.itheima.health.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author liu
 * @version 1.0.0
 * @createTime 2020年10月09日
 * @Description 菜单控制
 */
@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {

    @Reference
    private MenuService menuService;

    @RequestMapping("/getMenuList")
    public Result getMenuList(){

        String username = SecurityContextHolder.getContext().getAuthentication().getName();

        try {
            List<Menu> lists=menuService.getMenuList(username);
            return new Result(true,"菜单数据加载成功!",lists);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"菜单数据加载失败!");
        }
    }
    @RequestMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){

        PageResult pageResult=menuService.findPage(queryPageBean);


        return new Result(true,"菜单分页查询",pageResult);
    }
    @RequestMapping("findById")
    public Result findById(Integer id){
         log.info("[菜单数据查询]id={}",id);
       Menu menu=menuService.findById(id);
        return new Result(true,"菜单数据查询",menu);
    }
    @RequestMapping("add")
public Result add(@RequestBody Menu menu){
        menuService.add(menu);

        return new Result(true,"菜单添加成功");
}
@RequestMapping("/edit")
public Result edit(@RequestBody Menu menu){

        menuService.edit(menu);


        return  new Result(true,"菜单编辑成功");
}

@RequestMapping("delete")
public Result delete(Integer id){
        menuService.delete(id);

        return  new Result(true,"菜单删除成功");
}
@RequestMapping("findAll")
public Result findAll(){
        log.info("[菜单查询所有]");
     List<Menu> list= menuService.findAll();

        return  new Result(true,"查询所有成功",list);
}
}
